﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YJKRevitKernel.ElementSearchUtility.LinkDocumentSuite;
using YJKGeometryFuncs;

namespace YJKInspectModelFunc.ModelLayer
{
	public class BaseHostInRoomPacker : BaseElementPacker
	{
		public List<KeyValuePair<BaseRoomPacker, XYZ>> LstHostRoom
		{
			get
			{
				return this.m_lstKVPHostRoom;
			}
		}

		public List<KeyValuePair<Solid, ElementGeometryPacker>> LstSolids
		{
			get
			{
				return this.m_lstKVPSolids;
			}
		}

		public BaseHostInRoomPacker(Element inputElement, RevitLinkInstance useRevitLinkInstance = null, object[] otherValues = null) : base(inputElement, useRevitLinkInstance, otherValues)
		{
			this.m_lstKVPHostRoom = new List<KeyValuePair<BaseRoomPacker, XYZ>>();
			this.m_lstKVPSolids = new List<KeyValuePair<Solid, ElementGeometryPacker>>();
			GeometryElement geometryElement;
			if (!(inputElement is FamilyInstance))
			{
				geometryElement = inputElement.get_Geometry(new Options
				{
                    DetailLevel = (ViewDetailLevel)3
				});
			}
			else
			{
				geometryElement = (inputElement as FamilyInstance).GetOriginalGeometry(new Options
				{
                    DetailLevel = (ViewDetailLevel)3
				}).GetTransformed((inputElement as FamilyInstance).GetTotalTransform());
			}
			if (null != geometryElement && !(inputElement is Opening))
			{
				List<Solid> allSolid;
				if (base.ThisElement is Stairs)
				{
					allSolid = Utility.GetAllSolid(geometryElement, base.UseTransform, true);
				}
				else
				{
					allSolid = Utility.GetAllSolid(geometryElement, base.UseTransform, false);
				}
				using (List<Solid>.Enumerator enumerator = allSolid.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Solid solid = enumerator.Current;
						this.LstSolids.Add(new KeyValuePair<Solid, ElementGeometryPacker>(solid, LinkDocumentUtility.GetGeometryLimitValue(solid)));
					}
					return;
				}
			}
			if (inputElement is Opening)
			{
				List<XYZ> list = new List<XYZ>();
				foreach (object obj in (inputElement as Opening).BoundaryCurves)
				{
					Curve curve = (Curve)obj;
					if (base.UseRevitLinkInstance == null)
					{
						list.Add(curve.GetEndPoint(0));
						list.Add(curve.GetEndPoint(1));
					}
					else
					{
						list.Add(Geometry.TransformPoint(curve.GetEndPoint(0), base.UseTransform));
						list.Add(Geometry.TransformPoint(curve.GetEndPoint(1), base.UseTransform));
					}
				}
				this.LstSolids.Add(new KeyValuePair<Solid, ElementGeometryPacker>(null, LinkDocumentUtility.GetGeometryLimitValue(ref list)));
			}
		}

		public void AddHostRoom(BaseRoomPacker input, XYZ hostPoint)
		{
			if (input == null || hostPoint == null)
			{
				return;
			}
			if (this.m_lstKVPHostRoom == null)
			{
				this.m_lstKVPHostRoom = new List<KeyValuePair<BaseRoomPacker, XYZ>>();
			}
			bool flag = false;
			foreach (KeyValuePair<BaseRoomPacker, XYZ> keyValuePair in this.m_lstKVPHostRoom)
			{
				if (input.ThisElementPackerId == keyValuePair.Key.ThisElementPackerId)
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				this.m_lstKVPHostRoom.Add(new KeyValuePair<BaseRoomPacker, XYZ>(input, hostPoint));
			}
		}

		private List<KeyValuePair<BaseRoomPacker, XYZ>> m_lstKVPHostRoom;

		private List<KeyValuePair<Solid, ElementGeometryPacker>> m_lstKVPSolids;
	}
}
